<?php
namespace Admin\Controller;
use Think\Controller;
class BannerController extends CommonController {
    public function lists(){
        
        // $list = $this->Model->select();
        // $this->assign('list',$list);
        // $this->display();
        // $User  = M('User'); // 实例化User对象
        $count = $this->Model->relation(true)->count();// 查询满足要求的总记录数
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page ->setConfig('prev','上一页');

        $Page ->setConfig('next','下一页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('last', '尾页');



        $show  = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        // join('my_banner_point on my_banner_point.id = my_banne r.point_id')->
        $list = $this->Model->relation(true)->limit($Page->firstRow.','.$Page->listRows)->select();


        // print_r($list);die;
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }
    /**
     * 添加
     *
     * @return void
     * @author 
     **/
    function add()
    {
        
      if(IS_POST){
    // print_r($_FILES['img']);die;
       if(!empty($_FILES['img'])){

        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小   
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型   
        $upload->savePath  =      'banner/'; // 设置附件上传目录    

        // 上传文件     
        $info   =   $upload->uploadOne($_FILES['img']);    
        if(!$info) {
        // 上传错误提示错误信息        
        $this->error($upload->getError());  
        // echo 1 ;die;  
        }else{
          // echo 2;die;
        // 上传成功        
        // $this->success('上传成功！');
        $_POST['img'] = './Uploads/'.$info['savepath'].$info['savename'];
           }
         }

       // echo $banner_path;die;

           // 实例化表对象            
        $id = $this->Model->add(I('post.'));

         if($id){   
            $this->success('操作完成',U('banner/lists'),3);die;
         }else{
            $this->error('操作失败','',5);die;
            }
            
        }
      // 查询顶级数据

      $list = D('banner_point')->select();

      $this->assign('list',$list);

      // $list = $this->Model->where('pid=0')->select();
      // $this->assign('list',$list);

      $this->display();

  // echo 2;die;


    }

  function del()
      {
            // 实例化表对象
            
           $row = $this->Model->delect(I('get.id'));

            if($row){   
                 $this->success('操作完成',U('nav/lists'),3);die;
             }else{
                 $this->error('操作失败','/Article/error',5);die;
             }
            

    }
    function edit()
      {
        $this->display();
  }

  /**
   * undocumented function
   *
   * @return void
   * @author 
   **/
  function add_point()
   {

     if(IS_POST){
        $_POST['up_time'] = strtotime(I('post.up_time',0));
        $_POST['down_time'] = strtotime(I('post.down_time',0));
        // print_r(I('post.'));die;


        $id = D('banner_point')->add(I('post.'));

            if($id){   
                 $this->success('添加完成',U('admin/banner/lists_point'),3);die;
             }else{
                 $this->error('添加失败','/Article/error',5);die;
             }
     }
    $this->display();
  }
  /**
   * undocumented function
   *
   * @return void
   * @author 
   **/
  function lists_point()
  {
    $list =D('banner_point')->select();
    $this->assign('list',$list);
    $this->display();
  }
}